<template>
  <px-dialog v-model="visible" class="equip-dialog-wrap" title="选择备品备件/耗材" width="1280px" @open="openDialog" @close="closeDialog">
    <div class="equip-dialog-content">
      <px-form
        ref="formRef"
        class="custom-form-ui equip-form"
        :model="queryParams"
        label-position="right"
        label-width="120px"
        style="justify-content: start"
      >
        <px-form-item label="名称/编码">
          <px-input
            v-model="queryParams.sparePartCode"
            placeholder="请输入名称/编码"
            maxlength="50"
            clearable
            @blur="queryParams.sparePartCode = $event.target.value.trim()"
            @keyup.enter="queryTable"
          >
            <template #suffix>
              <px-icon style="cursor: pointer" @click="queryTable">
                <search />
              </px-icon>
            </template>
          </px-input>
        </px-form-item>
        <div class="equip-dialog-btn">
          <px-button type="primary" @click="queryTable">查询</px-button>
          <px-button @click="resetTable">重置</px-button>
        </div>
      </px-form>
      <px-table :data="tableData" class="equip-table" size="small" height="400" border @selection-change="selectionChange">
        <px-table-column type="selection" width="40" align="center" />
        <px-table-column
          v-for="(item, index) in tableColumns"
          :key="index"
          :label="item.label"
          :min-width="item.minWidth"
          :align="item.align"
          :show-overflow-tooltip="item.showOverflowTooltip"
          :fixed="item.fixed"
        >
          <template #default="scope">{{
            scope.row[item.prop] === 0 ? scope.row[item.prop] : scope.row[item.prop] ? scope.row[item.prop] : "-"
          }}</template>
        </px-table-column>
      </px-table>
      <pagination
        v-show="total > 0"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        class="equip-table-pagination"
        :total="total"
        @pagination="queryTable"
      />
      <div style="margin-top: 16px; text-align: right">
        <px-button @click="closeDialog">取消</px-button>
        <px-button type="primary" @click="handleSubmit">确定</px-button>
      </div>
    </div>
  </px-dialog>
</template>
<script setup lang="ts">
import { querySparePartsWarehousingInfo } from "@/api/equip/spareParts";
import { computed, reactive, toRefs } from "vue";

// 调用父级-关闭弹窗方法
const emits = defineEmits(["closeDialog", "addTableRow"]);
// 父级传过来的值
const props = defineProps(["visible", "className"]);
const visible = computed(() => props.visible);
// 页面本身的值
const state = reactive<any>({
  queryParams: {
    sparePartCode: "",
    pageNum: 1,
    pageSize: 20
  },
  tableData: [],
  total: 0,
  selectRows: [],
  tableColumns: [
    { prop: "categoryDesc", label: "种类", minWidth: "140", showOverflowTooltip: true, fixed: "left" },
    { prop: "sparePartCode", label: "编码", minWidth: "140", showOverflowTooltip: true },
    { prop: "sparePartName", label: "名称", minWidth: "140", showOverflowTooltip: true },
    { prop: "classNameDesc", label: "科室", minWidth: "140", showOverflowTooltip: true },
    { prop: "typeName", label: "类型", minWidth: "140", showOverflowTooltip: true },
    { prop: "brandName", label: "品牌", minWidth: "140", showOverflowTooltip: true },
    { prop: "supplyModel", label: "厂商型号", minWidth: "140", showOverflowTooltip: true },
    { prop: "model", label: "厂内型号", minWidth: "140", showOverflowTooltip: true }
  ]
});
const { queryParams, tableData, selectRows, tableColumns, total } = toRefs(state);
// 查询备件列表
const queryTable = async () => {
  const params = queryParams.value;
  const data = await querySparePartsWarehousingInfo(params);
  tableData.value = data?.data || [];
  total.value = data?.total;
};
// 重置筛选条件和备件列表
const resetTable = async () => {
  queryParams.value = {
    sparePartCode: "",
    className: props.className,
    pageNum: queryParams.value.pageNum,
    pageSize: queryParams.value.pageSize
  };
  queryTable();
};
// 表格多选框选中
const selectionChange = (val: any) => {
  selectRows.value = [];
  val.forEach((item: any) => {
    if (item.id) {
      selectRows.value.push(item);
    }
  });
};
// 确定选择备件
const handleSubmit = () => {
  emits("closeDialog");
  emits("addTableRow", selectRows.value);
};

// 打开弹窗
const openDialog = () => {
  queryParams.value = {
    sparePartCode: "",
    className: props.className,
    pageNum: 1,
    pageSize: 20
  };
  queryTable();
};
// 关闭弹窗
const closeDialog = () => {
  emits("closeDialog");
};
</script>
<style lang="scss" scoped>
@import url("@/views/all/equip/scss/add-dialog.scss");
:deep(.px-pagination) {
  position: absolute;
  right: 32px;
}
</style>
